package com.meixiang.activity.homes.insurance;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.meixiang.R;
import com.meixiang.adapter.InsuranceBuyAdapter;
import com.meixiang.adapter.InsuranceRecommedAdapter;
import com.meixiang.main.BaseActivity;
import com.meixiang.recyclerview.RecycleViewDivider;
import com.meixiang.view.TitleView;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author yinsujun  Created at 2016/5/3 17:02
 *         美保险
 */
public class InsuranceActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener,
        View.OnClickListener {

    @Bind(R.id.title)
    TitleView titleView; //标题
    @Bind(R.id.content)
    FrameLayout contentFrameLayout;
    @Bind(R.id.swipe_target)
    RecyclerView swipe_target;
    List<String> data;
    @Bind(R.id.mRefresh)
    SwipeToLoadLayout mRefresh;
    @Bind(R.id.ll_insurance_bottom)
    LinearLayout ll_insurance_bottom; //底部推荐按钮
    @Bind(R.id.ll_buy_bottom)
    LinearLayout ll_buy_bottom; //底部购买按钮
    @Bind(R.id.iv_buy)
    ImageView iv_buy;
    @Bind(R.id.tv_buy)
    TextView tv_buy;
    @Bind(R.id.iv_recommend)
    ImageView iv_recommend;
    @Bind(R.id.tv_recommend)
    TextView tv_recommend;
    private InsuranceRecommedAdapter adapter; //推荐数据适配
    private InsuranceBuyAdapter buyAdapter;// 购买数据适配

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_insurance);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        titleView.setTitle(R.string.insurance_title);
        titleView.setLeftImageButton(R.mipmap.back);
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleView.getRightTitleView().setOnClickListener(this);
        swipe_target.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        swipe_target.setItemAnimator(new DefaultItemAnimator());
        swipe_target.addItemDecoration(new RecycleViewDivider(context, RecycleViewDivider.HORIZONTAL_LIST));
        ll_insurance_bottom.setOnClickListener(this);
        ll_buy_bottom.setOnClickListener(this);
        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
        //自动刷新
        mRefresh.post(new Runnable() {
            @Override
            public void run() {
                mRefresh.setRefreshing(true);
            }
        });

        //RecyclerView滑到底部自动加载更多
        swipe_target.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (!ViewCompat.canScrollVertically(recyclerView, 1)) {
                        mRefresh.setLoadingMore(true);
                    }
                }
            }
        });

    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    data = new ArrayList<>();
                    for (int i = 0; i < 30; i++) {
                        data.add(i + "");
                    }
                    adapter.notifyDataSetChanged();
                    mRefresh.setRefreshing(false);
                    break;
                case 1:
                    if (data.size() > 50) {
                        Toast.makeText(context, "已没有更多数据", Toast.LENGTH_LONG).show();//設置已沒有更多数据
                    } else {
                        int size = data.size();
                        for (int i = size; i < size + 15; i++) {
                            data.add(i + "");
                        }
                        adapter.notifyDataSetChanged();
                    }
                    mRefresh.setLoadingMore(false);
                    break;
            }
        }
    };

    @Override
    public void onLoadMore() {
        mHandler.sendEmptyMessageDelayed(1, 1500);
    }

    @Override
    public void onRefresh() {
        mHandler.sendEmptyMessageDelayed(0, 1500);
    }

    @Override
    protected void initView() {

    }

    /**
     * 初始化数据
     */
    @Override
    protected void setData() {
        data = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            data.add(i + "");
        }
        adapter = new InsuranceRecommedAdapter(context,data);
        swipe_target.setAdapter(adapter);
    }

    /**
     * 推荐和购买切换
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        if(v.equals(titleView.getRightTitleView())){
            startActivity(new Intent(context,InsuranceHistoryActivity.class));
        }

        switch (v.getId()) {

            case R.id.ll_insurance_bottom:
                changInsuranceTextColorAndImage();
                break;

            case R.id.ll_buy_bottom:
                changBuyTextColorAndImage();
                break;
        }
    }


    /**
     * 点击购买时，改变相应的文字颜色和图片，重绘RecyclerView
     */
    private void changBuyTextColorAndImage() {
        tv_buy.setTextColor(ContextCompat.getColor(context, R.color.home_color_insurance));
        tv_recommend.setTextColor(ContextCompat.getColor(context,R.color.financial_gray_text_color));
        iv_buy.setImageResource(R.mipmap.buy_red);
        iv_recommend.setImageResource(R.mipmap.recommend);
        titleView.getRightTitleView().setVisibility(View.VISIBLE);
        titleView.setRightTextButton(R.string.insurance_history);
        titleView.setRightTextCorlr(R.color.black_6);
        swipe_target.invalidate();
        buyAdapter = new InsuranceBuyAdapter(context,data);
        swipe_target.setAdapter(buyAdapter);
    }

    /**
     *点击推荐时，改变相应的文字颜色和图片，重绘RecyclerView
     */
    private void changInsuranceTextColorAndImage() {
        tv_buy.setTextColor(ContextCompat.getColor(context, R.color.financial_gray_text_color));
        tv_recommend.setTextColor(ContextCompat.getColor(context,R.color.home_color_insurance));
        iv_buy.setImageResource(R.mipmap.buy);
        iv_recommend.setImageResource(R.mipmap.recommend_red);
        titleView.getRightTitleView().setVisibility(View.INVISIBLE);
        swipe_target.invalidate();
        swipe_target.setAdapter(adapter);
    }

}
